/*
    SpawnSurance - Minecraft Plugin for casualty insurance of inventory contents
    Copyright (C) 2012 Ernest Pazera

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
package com.pdg.mcplugin.spawnsurance;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.VaultAdapter;
import com.pdg.mcplugin.common.baseclasses.PluginBase;
import com.pdg.mcplugin.spawnsurance.commandprocessors.CommandProcessor;

public class SpawnSurance extends PluginBase {
	
	private static final String commandSpawnSurance = "SPAWNSURANCE";
	public Economy getEconomy(){
		return VaultAdapter.getEconomy();
	}
	
	//plugin manager for attaching eventes
	private PluginManager pluginManager = null;
	public PluginManager getPluginManager(){
		return pluginManager;
	}
	
	//data provider for talking to database
	private DataProvider dataProvider = null;
	public DataProvider getDataProvider(){
		return dataProvider;
	}
	
	//configuration
	private Configuration configuration = null;
	public Configuration getPluginConfiguration(){
		return configuration;
	}

	private PermissionChecker permissionChecker = null;
	public PermissionChecker getPermissionChecker(){
		if(permissionChecker==null){
			permissionChecker = new PermissionChecker(this);
		}
		return permissionChecker;
	}
	
	
	//server listener
	private Listener entityListener = null;
	private Listener playerListener = null;
	
	//handle commands
	public boolean onCommand(CommandSender theCommandSender, Command theCommand, String theLabel, String[] theArgs){
		ArgumentList theArgumentList = new ArgumentList(theArgs);
		if(theCommand.getName().equalsIgnoreCase(commandSpawnSurance)){
			return getCommandProcessor().processCommand(theCommandSender,theArgumentList);
		}
		return false;
	}
	
	private CommandProcessor commandProcessor=null;
	private CommandProcessor getCommandProcessor() {
		if(commandProcessor==null){
			commandProcessor = new CommandProcessor(this);
		}
		return commandProcessor;
	}
	
	@Override
	protected void disablePlugin() {
	}

	@Override
	protected boolean enablePlugin() {
		//grab the plugin
		pluginManager = this.getServer().getPluginManager();
		//create data provider
		dataProvider = new DataProvider(this);
		//create entity listener
		entityListener = new SpawnSuranceEntityListener(this);
		//create player listener
		playerListener = new SpawnSurancePlayerListener(this);
		//create configuration holder
		configuration = new Configuration(this);
		//register events
		pluginManager.registerEvents(entityListener, this);
		pluginManager.registerEvents(playerListener, this);
		return true;
	}

}
